package br.com.cfccultura.admin.control.actions.cadastros;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Component;

import br.com.cfccultura.admin.control.actions.DefaultAction;
import br.com.cfccultura.admin.control.forms.cadastros.CadastroServicoForm;
import br.com.cfccultura.admin.model.enums.Ativo;
import br.com.cfccultura.admin.model.vo.Servico;

@Component("/cadastroServico")
public class CadastroServicoAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(CadastroServicoAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		consultar(servicoForm, request);
		
		return mapping.findForward("body");
	}
	
	public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		consultar(servicoForm, request);
		
		return mapping.findForward("body");
	}
	
	public ActionForward incluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		Servico vo = new Servico();
		vo.setDescricao(servicoForm.getDescricao());
		vo.setValorCusto(DecimalFormat.getInstance(new Locale("pt","br")).parse(servicoForm.getValorCusto()).doubleValue());
		vo.setValorVenda(DecimalFormat.getInstance(new Locale("pt","br")).parse(servicoForm.getValorVenda()).doubleValue());
		getServicoDAO().incluir(vo);
		
		return null;
	}	
	
	public ActionForward alterarIniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		List<Servico> lista = getServicoDAO().consultar(new Servico(servicoForm.getIdSelecionado()));
		 
		Servico vo = lista.get(0);
		printJsonObjectToStream(vo, response);
		
		return null;
	}	
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		Servico vo = new Servico();
		vo.setCodigo(servicoForm.getIdSelecionado());
		vo.setDescricao(servicoForm.getDescricao());
		vo.setValorCusto(DecimalFormat.getInstance(new Locale("pt","br")).parse(servicoForm.getValorCusto()).doubleValue());
		vo.setValorVenda(DecimalFormat.getInstance(new Locale("pt","br")).parse(servicoForm.getValorVenda()).doubleValue());
		getServicoDAO().alterar(vo);
		
		return null;
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroServicoForm servicoForm = (CadastroServicoForm) form;
		getServicoDAO().excluir(servicoForm.getIdSelecionado());
		
		return null;
	}

	
	private void consultar(CadastroServicoForm servicoForm, HttpServletRequest request) {
		Servico vo = new Servico();
		vo.setAtivo(Ativo.setValue(servicoForm.getAtivoConsultar()));
		
		List<Servico> listaResultado = getServicoDAO().consultar(vo);
		request.setAttribute("listaResultado", listaResultado);
	}
}
